const Point = {
	map: null,
	points: {},
	circle: {},
	AMap: null,
	store: null,
	typeId: 3,
	sourceTypeData: {},
	// 标图片点
	setImgPoint(data, type) {
		const _this = this;
		Point.map?.clearMap();
		let markers = []
		for (let i = 0; i < data.length; i++) {
			const curData = data[i];
			curData.extData.index = i;
			const labelMarker = new AMap.Marker(curData);
				labelMarker.on('click', function (ev) {
					Point.circle && Point.map.remove([Point.circle]);
			//     //具体属性请查看文档
					let circle = new AMap.Circle({
						center: [ev.lnglat.lng,ev.lnglat.lat],
						radius: Number((Point.rangeValue || 1) + '000'), //半径
						borderWeight: 3,
						strokeColor: "#007acc", 
						strokeOpacity: 1,
						strokeWeight: 0,
						strokeOpacity: 0.2,
						fillOpacity: 0.4,
						strokeStyle: 'solid',
						strokeDasharray: [0,0,0], 
						// 线样式还支持 'dashed'
						fillColor: '#1791fc',
						zIndex: 50,
				});
				// 将圆形添加到地图上
				// circle.setMap(Point.map);
				Point.map.add(circle);
				Point.circle = circle
				Point.store.commit('common/SET_POINTID',curData.extData.id)
				Point.store.commit('common/SET_SHOWPAGE',2)
			// 缩放地图到合适的视野级别
				Point.map.setFitView([ circle ]);
				// Point.imgInitFn({ ...curData.extData, code: curData.name }, { x: ev.originEvent.x, y: ev.originEvent.y, position: ev.target.getPosition() })
			});
			markers.push(labelMarker);
		}
		Point.sourceTypeData[type] = markers;
		// 将 marker 添加到图层
		Point.map.add(markers);

	},
	clearPointKeys(key) {
		const markers = Point.sourceTypeData[key];
		Point.map?.remove(markers || []);
	},
	handlePointData(data, store) {
    const _this = this;
    let pointData = [];
		Point.store = store;
    _this.sourceData = { ..._this.sourceData, ...data };
    _this.curTypes = [];
    Object.entries(data).forEach((type, i) => {
      _this.curTypes.push(type[0]);
      pointData = [..._this.getTypeData(type[1], type[0])]
      _this.setImgPoint(pointData, type[0]);
    })
  },
	// 获取对应类型的数据
	getTypeData(typeData, type) {
		const _this = this;
		let typeDatas = [];
		for (let i = 0, len = typeData.length; i < len; i++) {
			let item = { promptType: type, ...typeData[i] };
			// video特殊处理
			if (item.gis) {
				const gis = item.gis.split(',');
				item.latitude = gis[1];
				item.longitude = gis[0];
			}
			item.lat && typeDatas.push(_this.getPointData(item, type, i));
		}
		return typeDatas;
	},
	 // 获取类型icon
	getTypeIcon(typename) {
		let icon = 'zh' // 综合
		if(typename.indexOf('政务') != -1) {
			icon = 'zw'
		} else if (typename.indexOf('银行') != -1) {
			icon = 'bank'
		} else if(typename.indexOf('街') != -1 || typename.indexOf('村')!= -1) {
			icon='county'
		} else if(typename.indexOf('社区')!=-1) {
			icon = 'community'
		}else if(typename.indexOf('专业分厅')!=-1) {
			icon = 'zy'
		}
		return new URL(`../static/mapinfo/${Point.typeId == 1 ? 'bank' : 'county'}.png`, import.meta.url).href
	},
 // 获取点数据
	getPointData(item, type, i, size) {
		const _this = this;
		const icon = _this.getTypeIcon(item.name);//type, item.iconActive);
		const iconObj = new AMap.Icon({
			// 图标类型，现阶段只支持 image 类型
			// type: 'image',
			// 图片 url
			image: icon,
			// 图片尺寸
			size: new AMap.Size(88, 126), //_this.getScaleSize(size || imgSize),
			imageOffset: new AMap.Pixel(22, 22),  // 图像相对展示区域的偏移量，适于雪碧图等
			imageSize: new AMap.Size(44, 63),  // 根据所设置的大小拉伸或压缩图片
			// 图片相对 position 的锚点，默认为 bottom-center
			anchor: 'center'
		})
		const textObj = {
			direction: 'right',
			// 在 direction 基础上的偏移量
			offset: [-20, -5],
			// 文字样式
			style: {
				// 字体大小
				fontSize: 12,
				// 字体颜色
				fillColor: '#22886f',
				//
				strokeColor: '#fff',
				strokeWidth: 2,
				fold: true,
				padding: '2, 5',
			}
		}
		// const textContent = type === 'oldAloneMan' ? `${item.villageName}(${item.userNum})` : type === 'communityNum' ? `${item.village_name}/检测数：${item.count || 0}` : (text || '');
		const ampObj = {
			name: `${type}_${i}`,
			position: new AMap.LngLat(item.lng, item.lat),
			zooms: [10, 20],
			opacity: 1,
			zIndex: 1000,
			fold: true,
			offset: new AMap.Pixel(-44, -63),
			icon: iconObj,
			title: 'textContent',
			text: { content: '', ...textObj },
			extData: item
		}
		return ampObj //_this.createType.value ==='img' ? obj : objMaker;
	}
}

export default Point;